package com.code.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.code.dao.ProductMapper;
import com.code.dao.OrderMapper;
import com.code.entity.OrderVo;
import com.code.entity.PageResult;
import com.code.entity.QueryPageBean;
import com.code.pojo.Product;
import com.code.pojo.Order;
import com.code.service.OrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    ProductMapper productMapper;


    @Override
    public PageResult<Order> findPage(QueryPageBean queryPageBean) {
     PageHelper.startPage(queryPageBean.getPage(),queryPageBean.getSize());
        List<Order> orderList = orderMapper.findLike(queryPageBean);
        PageInfo<Order> pageInfo = new PageInfo<>(orderList);
        PageResult<Order> result = new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
        return result;

    }
//    @Override
//    public PageResult<Order> findPage(Order order) {
//     PageHelper.startPage(order.getPage(),order.getSize());
//        List<Order> orderList = orderMapper.findLike(order);
//        PageInfo<Order> pageInfo = new PageInfo<>(orderList);
//        PageResult<Order> result = new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
//        return result;
//    }



    @Override
    public Order findById(Integer id) {
        return orderMapper.selectByPrimaryKey(id);
    }

    @Override
    public void add(Order order) {
        orderMapper.insert(order);
    }

    @Override
    public void update(Order order) {
        orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public void delete(Integer id) {
        orderMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Product> findProduct() {
        return productMapper.selectAll();
    }


//   统计用注解写Sql语句 字段名称为 name  value
    @Override
    public List<Map> tongji() {
        return orderMapper.tongji();
    }

//    @Override
//    public List<Map> twobzt() {
//        return orderMapper.twobzt();
////        return null;
//    }

    @Override
    public PageResult<Order> findPageNo(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getPage(),queryPageBean.getSize());
        List<Order> orderList = orderMapper.findPageNo(queryPageBean);
        PageInfo<Order> pageInfo = new PageInfo<>(orderList);
        PageResult<Order> result = new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
        return result;
    }

    @Override
    public void editState(Integer id) {
        Order order = orderMapper.selectByPrimaryKey(id);
        order.setState(1);
        orderMapper.updateByPrimaryKeySelective(order);
    }
}
